<?php
namespace app\staff\model;
use app\common\model\Common;
use think\Request;
use think\Db;
class OrderCount extends Common
{
	protected $table = 'mps_order';

	/**
	* 获取订单总数
	*/
	public function orderNum($where)
	{
		$sql = $this->alias('order')->field('order.order_id')
		->join('mps_order_goods goods','order.order_id = goods.order_id','LEFT')
		->where($where)->group('order.order_id')->select(false);
		// echo $sql;
		$mo = "($sql) as a";
		return Db::table($mo)->count();
	}
	/**
	* 订单商品统计
	*/
	public function countOrderGoods($where, $order = '', $search = '')
	{
		$orderGoods = $this->orderGoods($where, $order, $search);
		$order_money = 0;/*销售总额*/
		$buying = 0;/*销售总成本*/
		$sale = 0;/*销售总盈利*/
		$sale_bonus = 0;/*销售总提成*/
		$turn_bonus = 0;/*营业额总提成*/
		foreach($orderGoods as $k => $v){
			$order_money = $this->doubleSum($order_money, $v['goods_money']);
			$buying = $this->doubleSum($buying, $v['cost_money']);
			$sale = $this->doubleSum($sale, $v['profit_money']);
			$sale_bonus = $this->doubleSum($sale_bonus, $v['profit_t_money']);
			$turn_bonus = $this->doubleSum($turn_bonus, $v['turnover_t_money']);
		}
		$refund_money = $this->afterMoney($where, config('status.one_num'), $search);/*退款总额*/
		$recal_money = $this->afterMoney($where, config('status.two_num'), $search);/*召回总额*/
		$order_num = $this->orderNum($where);/*订单总数*/
		return ['order_money'=>$order_money,'buying'=>$buying,'sale'=>$sale,'sale_bonus'=>$sale_bonus,'turn_bonus'=>$turn_bonus,'refund_money'=>$refund_money,'recal_money'=>$recal_money,'order_num'=>$order_num];
	}
	/**
	* 订单所有商品统计
	*/
	public function allOrderGoods($where, $order = '', $search = '')
	{
		$order_id = $this->getAllOrder($where, $order, $search);
		$orderGoods = Db::name('orderGoods')->where('order_id','in',$order_id)->select();
		$order_money = 0;/*销售总额*/
		$buying = 0;/*销售总成本*/
		$sale = 0;/*销售总盈利*/
		$sale_bonus = 0;/*销售总提成*/
		$turn_bonus = 0;/*营业额总提成*/
		foreach($orderGoods as $k => $v){
			$order_money = $this->doubleSum($order_money, $v['goods_money']);
			$buying = $this->doubleSum($buying, $v['cost_money']);
			$sale = $this->doubleSum($sale, $v['profit_money']);
			$sale_bonus = $this->doubleSum($sale_bonus, $v['profit_t_money']);
			$turn_bonus = $this->doubleSum($turn_bonus, $v['turnover_t_money']);
		}
		$refund_money = $this->afterMoney($where, config('status.one_num'), $search);/*退款总额*/
		$recal_money = $this->afterMoney($where, config('status.two_num'), $search);/*召回总额*/
		$order_num = $this->orderNum($where);/*订单总数*/
		return ['order_money'=>$order_money,'buying'=>$buying,'sale'=>$sale,'sale_bonus'=>$sale_bonus,'turn_bonus'=>$turn_bonus,'refund_money'=>$refund_money,'recal_money'=>$recal_money,'order_num'=>$order_num];
	}
	/**
	* 订单商品
	*/
	public function orderGoods($where, $order, $search)
	{
		return $this->alias('order')
		->join('mps_order_goods goods','order.order_id = goods.order_id','LEFT')
		->where($where)->where($search)->order($order)
		->group('goods.order_goods_id')->select();
	}
	
	/********************************** 私有 ***********************************/
	/**
	* 不带分页订单列表
	*/
	private function getAllOrder($where, $order, $search)
	{
		$order = $this->alias('order')
		->join('mps_order_goods goods','order.order_id = goods.order_id','LEFT')
		->join('mps_admin admin','admin.admin_id = order.admin_id','LEFT')
		->field('order.order_id')
		->where($where)
		->where($search)
		->order($order)
		->group('order.order_id')
		->select();
		if(!$order) return [];
		$order_id = [];
		foreach($order as $v){
			$order_id[] = $v['order_id'];
		}
		return implode(',', $order_id);
	}
	/**
	* 退款|召回总额
	* @param array 	$where 			条件
	* @param intger $after_type 	售后单类型
	*/
	private function afterMoney($where, $after_type, $search)
	{
		return $this->alias('order')
		->join('mps_order_goods goods','order.order_id = goods.order_id','LEFT')
		->where($where)->where('goods.is_show',$after_type)->where($search)
		->sum('goods.goods_money');
	}
	/**
	* 销售总成本
	* @param booleen 	$buying 		销售成本
	* @param booleen 	$buying_price 	成本价
	* @param intger 	$number 		商品个数
	*/
	private function buying($buying, $buying_price, $number)
	{
		return ( intval(strval($buying * 100)) + intval(strval($buying_price*100)) * intval(strval($number)) )/100;
	}
	/**
	* 销售总额
	* @param booleen 	$order_money 	销售总额
	* @param booleen 	$price 			商品卖价-销售价|批发价
	* @param intger 	$number 		商品个数
	*/
	private function order_money($order_money, $price, $number)
	{
		return ( intval(strval($order_money * 100)) + intval(strval($price*100)) * intval(strval($number)) )/100;
	}
	/**
	* 销售总盈利
	* @param booleen 	$order_money 		销售总额
	* @param booleen 	$buying 			销售成本
	*/
	private function sale($order_money, $buying)
	{
		return intval(strval($order_money)) - intval(strval($buying));
	}
	/**
	* 销售总提成
	* @param booleen 	$sale_bonus 	销售总提成
	* @param booleen 	$buying 		商品成本价
	* @param booleen 	$price 			商品卖价-销售价|批发价
	* @param intger 	$number 		商品个数
	* @param intger 	$profit_bonus 	销售提成比例
	*/
	private function sale_bonus($sale_bonus, $buying, $price, $number, $profit_bonus)
	{
		/*@单个商品销售盈利-整数(*100)*/
		$goods = ( intval(strval($price*100)) - intval(strval($buying*100)) ) * intval(strval($number));
		/*@单个商品销售提成*/
		$sale = intval(strval($goods)) * intval(strval($profit_bonus)) / 10000;
		/*@销售总提成*/
		return (intval(strval($sale_bonus*100))+intval(floor($sale*100)))/100;
	}
	/**
	* 营业额总提成
	* @param booleen 	$turn_bonus 	营业额总提成
	* @param booleen 	$price 			商品卖价-销售价|批发价
	* @param intger 	$number 		商品个数
	* @param intger 	$turnover_bonus 营业额提成比例
	*/
	private function turn_bonus($turn_bonus, $price, $number, $turnover_bonus)
	{
		/*@单个商品销售总额*/
		$goods = intval(strval($price*100))  * intval(strval($number));
		/*@单个商品营业额提成*/
		$turn = intval(strval($goods)) * intval(strval($turnover_bonus)) / 10000;
		/*@营业额总提成*/
		return (intval(strval($turn_bonus*100))+intval(floor($turn*100)))/100;
	}
}